//
//  ConfirmOrderTableViewCell.m
//  AnXin100
//
//  Created by Sakura on 2018/11/2.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "ConfirmOrderTableViewCell.h"
#import "OrderDetailProductTableViewCell.h"
#import "InvoiceViewController.h"
@interface ConfirmOrderTableViewCell ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *mainTable;

@property (nonatomic, strong) UILabel *storeNameLabel;

@property (nonatomic, strong) UIImageView *storeIconImage;

@property (nonatomic, strong) UIView *headView;

@property (nonatomic, strong) UIView *footView;

@end

@implementation ConfirmOrderTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor colorWithHex:0xf5f5f5];
        [self creatCell];
    }
    return self;
}

- (void)creatCell {
    
    _mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    _mainTable.delegate = self;
    _mainTable.dataSource = self;
    _mainTable.backgroundColor = [UIColor whiteColor];
    _mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:_mainTable];
    
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    _headView.backgroundColor = [UIColor whiteColor];
    _mainTable.tableHeaderView = _headView;
    
    _storeIconImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
    _storeIconImage.image = [UIImage imageNamed:@"grape"];
    [_headView addSubview:_storeIconImage];
    
    _storeNameLabel = [[UILabel alloc] init];
    _storeNameLabel.text = @"思维数码店铺";
    _storeNameLabel.font = [UIFont systemFontOfSize:16];
    _storeNameLabel.textColor = [UIColor colorWithHex:0x333333];
    [_headView addSubview:_storeNameLabel];
    
    _storeNameLabel.sd_layout
    .leftSpaceToView(_storeIconImage, 10)
    .rightSpaceToView(_headView, 10)
    .topEqualToView(_storeIconImage)
    .heightIs(30);
    
    _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    _footView.backgroundColor = [UIColor whiteColor];
    _mainTable.tableFooterView = _footView;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 90, 20)];
    titleLabel.text = @"买家留言：";
    titleLabel.textColor = [UIColor colorWithHex:0x333333];
    titleLabel.font = [UIFont systemFontOfSize:14];
    [_footView addSubview:titleLabel];
    
    UITextField *textField = [[UITextField alloc] init];
    textField.font = [UIFont systemFontOfSize:14];
    textField.placeholder = @"选填：填写内容已和卖家协商确认";
    [_footView addSubview:textField];
    
    textField.sd_layout
    .leftSpaceToView(titleLabel, 10)
    .rightSpaceToView(_footView, 10)
    .topEqualToView(titleLabel)
    .heightIs(20);
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, 39, SCREEN_WIDTH - 30, 1)];
    lineView.backgroundColor = [UIColor colorWithHex:0xf6f6f6];
    [_footView addSubview:lineView];
}



- (void)layoutSubviews {
    [super layoutSubviews];
    _mainTable.frame = CGRectMake(0, 0, self.mj_w, self.mj_h - 10);
}



- (void)setProductAry:(NSMutableArray *)productAry {
    _productAry = productAry;
    [_mainTable reloadData];
}

- (void)setTypeAry:(NSMutableArray *)typeAry {
    _typeAry = typeAry;
    [_mainTable reloadData];
}

#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _typeAry.count + _productAry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < _productAry.count) {
        
        return 95;
    } else {
        return 40;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row < _productAry.count) {
        OrderDetailProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderDetailProductTableViewCell"];
//        cell.backgroundColor = [UIColor colorWithHex:0xe6e6e6];
        if (!cell) {
            cell = [[OrderDetailProductTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OrderDetailProductTableViewCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    } else {
        
        NSDictionary *dic = _typeAry[indexPath.row - _productAry.count];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"UITableViewCell"];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = [dic allKeys][0];
        cell.textLabel.font = FONT_FOURTEEN;
        cell.textLabel.textColor = [UIColor colorWithHex:0x333333];
        cell.detailTextLabel.text = [dic allValues][0];
        cell.detailTextLabel.font = FONT_FOURTEEN;
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, 39, SCREEN_WIDTH - 30, 1)];
        lineView.backgroundColor = [UIColor colorWithHex:0xf6f6f6];
        [cell addSubview:lineView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
        if (indexPath.row == 4) {
            
            self.invoiceClick();
        }
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
